/**
 * Created by wyy on 17-7-10.
 */
angular.module('tribalc').factory('ElePosition', function () {



    function getElementLeft(element) {
        var actualLeft = element.offsetLeft;
        var current = element.offsetParent;
        while (current !== null) {
            actualLeft += current.offsetLeft;
            current = current.offsetParent;
        }
        return actualLeft;
    }

    function getElementTop(element) {
        var actualTop = element.offsetTop;
        var current = element.offsetParent;
        while (current !== null) {
            actualTop += current.offsetTop;
            current = current.offsetParent;
        }
        return actualTop;
    }

    function getElementViewLeft(element) {
        var actualLeft = getElementLeft(element);
        var elementScrollLeft;
        if (document.compatMode == "BackCompat") {
            elementScrollLeft = document.body.scrollLeft;
        } else {
            elementScrollLeft = document.documentElement.scrollLeft;
        }
        return actualLeft - elementScrollLeft;
    }

    function getElementViewTop(element) {
        var actualTop = getElementTop(element);
        var elementScrollTop;
        if (document.compatMode == 'BackCompat') {
            elementScrollTop = document.body.scrollTop;
        } else {
            elementScrollTop = document.documentElement.scrollTop;
        }
        return actualTop - elementScrollTop;
    }


    function getScrollTop() {
        var scrollTop=0;
        if(document.documentElement&&document.documentElement.scrollTop)
        {
            scrollTop=document.documentElement.scrollTop;
        }
        else if(document.body)
        {
            scrollTop=document.body.scrollTop;
        }
        return scrollTop;
    }


    return {
        getElementLeft: getElementLeft,
        getElementTop: getElementTop,
        getElementViewLeft: getElementViewLeft,
        getElementViewTop: getElementViewTop,
        getScrollTop: getScrollTop
    };


});
